"""
Markdown预处理脚本
供Java端调用，处理Markdown文件预处理请求
"""

import sys
import logging
from pathlib import Path

# 添加父目录到Python路径以便导入
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from md2ppt.integration.java_bridge import JavaBridge

def main():
    """预处理脚本主入口"""
    if len(sys.argv) < 2:
        print("用法: python preprocess.py <request_file>")
        sys.exit(1)
    
    request_file = Path(sys.argv[1])
    response_file = request_file.parent / f"{request_file.stem}_response.json"
    
    # 设置日志
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )
    
    logger = logging.getLogger(__name__)
    logger.info(f"开始处理预处理请求: {request_file}")
    
    # 使用JavaBridge处理请求
    bridge = JavaBridge()
    success = bridge.process_java_request(request_file, response_file)
    
    if success:
        logger.info(f"预处理完成，响应文件: {response_file}")
        sys.exit(0)
    else:
        logger.error("预处理失败")
        sys.exit(1)

if __name__ == "__main__":
    main()